package net.sf.cldsql.dj.utl;

/**
 * StrUtil
 */
public class StrUtl {
    /** singleton */
    private static final StrUtl instance = new StrUtl();

    /** conv char map underscored to capitalized */
    private static final String[][] UNDERSCORED_TO_CAPITALIZED_CHAR_MAP = {
            {"_a", "A"},
            {"_b", "B"},
            {"_c", "C"},
            {"_d", "D"},
            {"_e", "E"},
            {"_f", "F"},
            {"_g", "G"},
            {"_h", "H"},
            {"_i", "I"},
            {"_j", "J"},
            {"_k", "K"},
            {"_l", "L"},
            {"_m", "M"},
            {"_n", "N"},
            {"_o", "O"},
            {"_p", "P"},
            {"_q", "Q"},
            {"_r", "R"},
            {"_s", "S"},
            {"_t", "T"},
            {"_u", "U"},
            {"_v", "V"},
            {"_w", "W"},
            {"_x", "X"},
            {"_y", "Y"},
            {"_z", "Z"}
    };

    /** get singleton */
    public static StrUtl get() {
        return instance;
    }
    
    /**
     * conver SQL lower underscored naming convention to Java upper camel case naming convention
     * @param name
     * @return
     */
    public String convSqlToUJava(String name) {
        String ret = convSqlToLJava(name);
        if (ret != null && ret.length() >= 1) {
            ret = ret.substring(0, 1).toUpperCase() + ret.substring(1); 
        }
        return ret;
    }

    /**
     * conver SQL lower underscored naming convention to Java lower camel case naming convention
     * @param name
     * @return
     */
    public String convSqlToLJava(String name) {
        String ret = null;
        if (name != null) {
            ret = name.toLowerCase();
            for (String[] elem : UNDERSCORED_TO_CAPITALIZED_CHAR_MAP) {
                ret = ret.replaceAll(elem[0], elem[1]);
            }
        }
        return ret;
    }
    
    /**
     * conver Java upper camel case naming convention to SQL lower underscored naming convention
     * @param name
     * @return
     */
    public String convLJavaToSql(String name) {
        String ret = null;
        if (name != null) {
            ret = name;
            for (String[] elem : UNDERSCORED_TO_CAPITALIZED_CHAR_MAP) {
                ret = ret.replaceAll(elem[1], elem[0]);
            }
            ret = ret.toUpperCase();
        }
        return ret;
    }
    
    public String flattenCrLf(String str) {
        return str.replace("\r", "\\r").replace("\n", "\\n");
    }
}
